package com.ruoyi.hcrf.domain.service.agv.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.ruoyi.hcrf.bo.agv.TaskAgvBo;
import com.ruoyi.hcrf.bo.agv.TaskAgvInsert;
import com.ruoyi.hcrf.domain.StockFlow;
import com.ruoyi.hcrf.domain.StockMaster;
import com.ruoyi.hcrf.domain.entity.task.AgvMonious;
import com.ruoyi.hcrf.domain.entity.task.InOut;
import com.ruoyi.hcrf.domain.entity.task.InventoryRecordLog;
import com.ruoyi.hcrf.domain.entity.task.TaskAgv;
import com.ruoyi.hcrf.domain.mapper.agv.AgvMoniousMapper;
import com.ruoyi.hcrf.domain.mapper.agv.InOutMapper;
import com.ruoyi.hcrf.domain.mapper.agv.InventoryRecordLogMapper;
import com.ruoyi.hcrf.domain.mapper.agv.TaskAgvMapper;
import com.ruoyi.hcrf.domain.mapper.storage.StockFlowLogMapper;
import com.ruoyi.hcrf.domain.mapper.storage.StockMasterMapper;
import com.ruoyi.hcrf.domain.service.agv.TaskAgvService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.UUID;

@Service
public class TaskAgvServiceImpl extends ServiceImpl<TaskAgvMapper, TaskAgv> implements TaskAgvService {

    @Autowired
    private TaskAgvMapper taskAgvMapper;

    @Autowired
    private StockMasterMapper stockMasterMapper;

    @Autowired
    private StockFlowLogMapper stockFlowLogMapper;

    @Autowired
    private InventoryRecordLogMapper inventoryRecordLogMapper;

    @Autowired
    private InOutMapper inOutMapper;

    @Autowired
    private AgvMoniousMapper agvMoniousMapper;

    //查询在库表数据
    @Override
    public StockFlow selectFlow() {

        //查询在库表
        QueryWrapper<StockFlow> queryWrapper = new QueryWrapper<>();
        //该库区下-有货-先入先出--临时保管室
        //根据开捆箱id升序查询一条数据
        queryWrapper.orderByAsc("box_id")
                .eq("ware_house", 1)
                .eq("stock_flow_type", 2)
                .eq("status", 0)
                .eq("del_flag", 0)
                .last("limit 1");
        return stockFlowLogMapper.selectOne(queryWrapper);
    }

    //查询库位信息-起点临时保管室
    @Override
    public StockMaster select(String stockMasterId) {

        //根据参数库区
        QueryWrapper<StockMaster> queryWrapper = new QueryWrapper<>();
        //该库区下-有货-先入先出
        queryWrapper.eq("location_id", stockMasterId).eq("del_flag", 0);
//                .and(wrapper -> wrapper.eq("stock_flow_type", 2).or().eq("stock_flow_type", 3));
        return stockMasterMapper.selectOne(queryWrapper);

    }

    //查询库位信息-终点生药卸货室
    @Override
    public StockMaster selectTo() {
        //根据参数库区
        QueryWrapper<StockMaster> queryWrapper = new QueryWrapper<>();
        //
        queryWrapper.eq("ware_house", 2)
                .eq("storage_status", 0)
                .eq("storage_enabled", 0)
                .orderByAsc("layers")
                .orderByAsc("location_goods")
                .last("limit 1");
        return stockMasterMapper.selectOne(queryWrapper);
    }

    //生成agv任务-1-临时到生药
    @Override
    public boolean insert(TaskAgvInsert taskAgvInsert) {

        TaskAgv taskAgv = new TaskAgv();
        taskAgv.setTaskId(taskAgvInsert.getTaskId());
        taskAgv.setFromSite(taskAgvInsert.getFromSite());
        taskAgv.setToSite(taskAgvInsert.getToSite());
        taskAgv.setStatus(0);
        //在库id
        taskAgv.setFlowId(taskAgvInsert.getStockId());
        taskAgv.setBoxId(taskAgvInsert.getBoxId());
        taskAgv.setLocationId(taskAgvInsert.getLocationId());
        taskAgv.setCreateTime(new Date());
        int insert = taskAgvMapper.insert(taskAgv);
        //
        if (insert > 0) {
            //将库位状态改为已占用
            QueryWrapper<StockMaster> queryWrapper = new QueryWrapper<>();
            //
            queryWrapper.eq("location_id", taskAgvInsert.getToSite());
            StockMaster stockMaster = stockMasterMapper.selectOne(queryWrapper);
            //将终点库位状态改为已占用
            stockMaster.setStorageEnabled(1);
            stockMasterMapper.updateById(stockMaster);

            if (taskAgvInsert.getStockId() != null) {
                QueryWrapper<StockFlow> wrapper = new QueryWrapper<>();
                //该库区下-有货-先入先出
                wrapper.eq("stock_id", taskAgvInsert.getStockId());
                StockFlow stockFlow = stockFlowLogMapper.selectOne(wrapper);
                stockFlow.setStatus(1);
                stockFlowLogMapper.updateById(stockFlow);
            }
        }

        return insert > 0;
    }

    //根据开捆箱id查询在库表数据
    @Override
    public AgvMonious selectFlowById(String boxId) {

        //查询在库表
        QueryWrapper<AgvMonious> queryWrapper = new QueryWrapper<>();
        //生药卸货室 -- 终点裁切室 用法
        queryWrapper.eq("item_no", boxId).eq("del_flag", 0);

        return agvMoniousMapper.selectOne(queryWrapper);
    }

    //查询生药库位信息
    @Override
    public StockFlow selectFlowTwo() {
        //查询在库表
        QueryWrapper<StockFlow> queryWrapper = new QueryWrapper<>();
        //该库区下-有货-先入先出
        queryWrapper.eq("ware_house", 2)
                .eq("stock_flow_type", 1)
                .eq("status",0)
                .eq("del_flag", 0)
                .last("limit 1");
        return stockFlowLogMapper.selectOne(queryWrapper);
    }

    //查询生药库位信息-终点生药卸货室
    @Override
    public StockMaster selectToTwo() {
        //根据参数库区
        QueryWrapper<StockMaster> queryWrapper = new QueryWrapper<>();
        //放货点位
        queryWrapper.eq("ware_house", 1)
                .eq("storage_status", 0)
                .eq("storage_enabled", 0)
                .orderByAsc("layers")
                .orderByAsc("location_goods")
                .last("limit 1");
        return stockMasterMapper.selectOne(queryWrapper);
    }

    //根据taskId修改Agv任务
    @Override
    public Boolean findById(String taskId) {
        //根据task_id查询数据
        QueryWrapper<TaskAgv> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("task_id", taskId);
        TaskAgv taskAgv = taskAgvMapper.selectOne(queryWrapper);
        taskAgv.setStatus(1);
        int i = taskAgvMapper.updateById(taskAgv);
        return i > 0;
    }

    //根据taskId修改Agv任务-已完成
    @Override
    public Boolean findByIdOk(String taskId) {
        QueryWrapper<TaskAgv> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("task_id", taskId);
        TaskAgv taskAgv = taskAgvMapper.selectOne(queryWrapper);
        taskAgv.setStatus(2);
        int i = taskAgvMapper.updateById(taskAgv);
        if (i > 0) {
            //删除临时区在库 信息
            QueryWrapper<StockFlow> Wrapper = new QueryWrapper<>();
            Wrapper.eq("stock_id", taskAgv.getFlowId()).eq("del_flag", 0);
            StockFlow stockFlows = stockFlowLogMapper.selectOne(Wrapper);
            double qty = stockFlows.getActualQuantity();
            stockFlows.setDelFlag(1);
            stockFlows.setUpdateTime(new Date());
            int i1 = stockFlowLogMapper.updateById(stockFlows);
            if (i1 > 0) {
                //将库位状态改为未占用
                QueryWrapper<StockMaster> queryWrapper1 = new QueryWrapper<>();
                queryWrapper1.eq("location_id", taskAgv.getFromSite());
                StockMaster stockMaster = stockMasterMapper.selectOne(queryWrapper1);
                stockMaster.setStorageEnabled(0);
                stockMaster.setStorageStatus(0);
                int i2 = stockMasterMapper.updateById(stockMaster);
                if (i2 > 0) {
                    //生成履历
                    InventoryRecordLog inventoryRecordLog = new InventoryRecordLog();
                    inventoryRecordLog.setId(UUID.randomUUID().toString());
                    inventoryRecordLog.setFlowType(stockFlows.getStockFlowType());
                    inventoryRecordLog.setBoxId(taskAgv.getBoxId());
                    inventoryRecordLog.setItemName(stockFlows.getItemName());
                    //临时保管室
                    inventoryRecordLog.setWareHouse(1);
                    inventoryRecordLog.setLocationId(taskAgv.getFromSite());
                    inventoryRecordLog.setQty(qty);
                    inventoryRecordLog.setCreateTime(new Date());
                    inventoryRecordLogMapper.insert(inventoryRecordLog);
                }
            }
            //新增卸货区 在库 信息
            StockFlow stockFlow = new StockFlow();
            stockFlow.setStockId(UUID.randomUUID().toString());
            stockFlow.setBoxId(taskAgv.getBoxId());
            stockFlow.setItemName(stockFlows.getItemName());
            stockFlow.setLocationId(taskAgv.getToSite());
            stockFlow.setWareHouse(2);
            stockFlow.setActualQuantity(qty);
            //入库
            //2-有货
            stockFlow.setStockFlowType(2);
            stockFlow.setStatus(0);
            stockFlow.setStockTime(new Date());
            stockFlow.setCreateTime(new Date());
            int i2 = stockFlowLogMapper.insert(stockFlow);
            if (i2 > 0) {
                //将库位状态改为已占用
                QueryWrapper<StockMaster> queryWrapper1 = new QueryWrapper<>();
                queryWrapper1.eq("location_id", taskAgv.getToSite());
                StockMaster stockMaster = stockMasterMapper.selectOne(queryWrapper1);
                stockMaster.setStorageEnabled(1);
                stockMaster.setStorageStatus(2);
                int i3 = stockMasterMapper.updateById(stockMaster);
                if (i3 > 0) {
                    //生成出入库履历数据
                    InventoryRecordLog inventoryRecordLog = new InventoryRecordLog();
                    inventoryRecordLog.setId(UUID.randomUUID().toString());
                    inventoryRecordLog.setFlowType(2);
                    inventoryRecordLog.setBoxId(taskAgv.getBoxId());
                    inventoryRecordLog.setItemName(stockFlows.getItemName());
                    inventoryRecordLog.setWareHouse(2);
                    inventoryRecordLog.setLocationId(taskAgv.getToSite());
                    inventoryRecordLog.setQty(qty);
                    inventoryRecordLog.setCreateTime(new Date());
                    inventoryRecordLogMapper.insert(inventoryRecordLog);
                }
            }
        }
        return i > 0;
    }

    //根据taskId修改Agv任务-已完成-生药到裁切
    @Override
    public Boolean findByIdOkTwo(String taskId) {
        //删除卸货区 在库 信息
        QueryWrapper<TaskAgv> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("task_id", taskId);
        TaskAgv taskAgv = taskAgvMapper.selectOne(queryWrapper);
        taskAgv.setStatus(2);
        int i = taskAgvMapper.updateById(taskAgv);
        int i3 =0 ;
        if (i > 0) {
            //删除卸货区 在库 信息
            QueryWrapper<StockFlow> Wrapper = new QueryWrapper<>();
            Wrapper.eq("stock_id", taskAgv.getFlowId()).eq("del_flag", 0);
            StockFlow stockFlows = stockFlowLogMapper.selectOne(Wrapper);
            double a = stockFlows.getActualQuantity();
            stockFlows.setDelFlag(1);
            stockFlows.setUpdateTime(new Date());
            int i1 = stockFlowLogMapper.updateById(stockFlows);
            if (i1 > 0) {
                //将库位状态改为未占用
                QueryWrapper<StockMaster> queryWrapper1 = new QueryWrapper<>();
                queryWrapper1.eq("location_id", taskAgv.getLocationId());
                StockMaster stockMaster = stockMasterMapper.selectOne(queryWrapper1);
                stockMaster.setStorageEnabled(0);
                stockMaster.setStorageStatus(0);
                int i2 = stockMasterMapper.updateById(stockMaster);
                if (i2 > 0) {
                    InventoryRecordLog inventoryRecordLog = new InventoryRecordLog();
                    inventoryRecordLog.setId(UUID.randomUUID().toString());
                    inventoryRecordLog.setFlowType(1);
                    inventoryRecordLog.setBoxId(taskAgv.getBoxId());
                    inventoryRecordLog.setItemName(stockFlows.getItemName());
                    //生药保管室
                    inventoryRecordLog.setWareHouse(2);
                    inventoryRecordLog.setLocationId(taskAgv.getFromSite());
                    inventoryRecordLog.setQty(a);
                    inventoryRecordLog.setCreateTime(new Date());
                    inventoryRecordLogMapper.insert(inventoryRecordLog);
                }
            }
            //新增裁切区 在库 信息
//            StockFlow stockFlow = new StockFlow();
//            stockFlow.setStockId(UUID.randomUUID().toString());
//            stockFlow.setBoxId(taskAgv.getBoxId());
//            stockFlow.setItemName(stockFlows.getItemName());
//            stockFlow.setLocationId(taskAgv.getToSite());
//            stockFlow.setWareHouse(3);
//            stockFlow.setStockQty(a);
//            stockFlow.setStatus(0);
//            stockFlow.setStockFlowType(stockFlows.getStockFlowType());
//            stockFlow.setStockTime(new Date());
//            stockFlow.setCreateTime(new Date());
//            int i2 = stockFlowLogMapper.insert(stockFlow);
                //将库位状态改为已占用
                QueryWrapper<StockMaster> queryWrapper1 = new QueryWrapper<>();
                queryWrapper1.eq("location_id", taskAgv.getToSite());
                StockMaster stockMaster = stockMasterMapper.selectOne(queryWrapper1);
                stockMaster.setStorageEnabled(1);
                stockMaster.setStorageStatus(2);
                i3 = stockMasterMapper.updateById(stockMaster);
                if (i3 > 0) {
                    //生成出入库履历数据
                    InventoryRecordLog inventoryRecordLog = new InventoryRecordLog();
                    inventoryRecordLog.setId(UUID.randomUUID().toString());
                    inventoryRecordLog.setFlowType(stockFlows.getStockFlowType());
                    inventoryRecordLog.setBoxId(taskAgv.getBoxId());
                    inventoryRecordLog.setItemName(stockFlows.getItemName());
                    inventoryRecordLog.setWareHouse(3);
                    inventoryRecordLog.setLocationId(taskAgv.getToSite());
                    inventoryRecordLog.setQty(a);
                    inventoryRecordLog.setCreateTime(new Date());
                    inventoryRecordLogMapper.insert(inventoryRecordLog);

            }
        }
        return i3 > 0;
    }

    //根据taskId修改Agv任务-已完成-裁切到卸货
    @Override
    public Boolean findByIdOkThree(TaskAgvBo taskAgvBo) {
        //
        QueryWrapper<TaskAgv> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("task_id", taskAgvBo.getTaskId());
        TaskAgv taskAgv = taskAgvMapper.selectOne(queryWrapper);
        taskAgv.setStatus(2);
        int i = taskAgvMapper.updateById(taskAgv);
        int aa = 0;
        if (i > 0) {
                //将库位状态改为未占用
                QueryWrapper<StockMaster> queryWrapper1 = new QueryWrapper<>();
                //
                queryWrapper1.eq("location_id", taskAgv.getFromSite());
                StockMaster stockMaster = stockMasterMapper.selectOne(queryWrapper1);
                stockMaster.setStorageEnabled(0);
                stockMaster.setStorageStatus(0);
                int i2 = stockMasterMapper.updateById(stockMaster);
                if (i2 > 0) {
                    InventoryRecordLog inventoryRecordLog = new InventoryRecordLog();
                    inventoryRecordLog.setId(UUID.randomUUID().toString());
                    inventoryRecordLog.setFlowType(taskAgvBo.getResult());
                    inventoryRecordLog.setBoxId(taskAgv.getBoxId());
                    inventoryRecordLog.setItemName(taskAgvBo.getItemName());
                    //裁切保管室
                    inventoryRecordLog.setWareHouse(3);
                    inventoryRecordLog.setLocationId(taskAgv.getFromSite());
                    inventoryRecordLog.setQty(taskAgvBo.getStockQty());
                    inventoryRecordLog.setCreateTime(new Date());
                    inventoryRecordLogMapper.insert(inventoryRecordLog);
//                }
            }
            //新增卸货区 在库 信息
            StockFlow stockFlow = new StockFlow();
            stockFlow.setStockId(UUID.randomUUID().toString());
            stockFlow.setBoxId(taskAgvBo.getBoxId());
            stockFlow.setItemName(taskAgvBo.getItemName());
            stockFlow.setLocationId(taskAgv.getToSite());
            stockFlow.setWareHouse(2);
            //0-未占用 1-已占用
            stockFlow.setStatus(0);
            stockFlow.setStockFlowType(taskAgvBo.getResult());
            //入库数量再定
            stockFlow.setActualQuantity(taskAgvBo.getStockQty());
            //入库时间
            stockFlow.setStockTime(new Date());
            //创建时间
            stockFlow.setCreateTime(new Date());
            int i3 = stockFlowLogMapper.insert(stockFlow);
            if (i3 > 0) {
                //将库位状态改为已占用
                QueryWrapper<StockMaster> stockMasterQueryWrapper = new QueryWrapper<>();
                stockMasterQueryWrapper.eq("location_id", taskAgv.getToSite());
                StockMaster master = stockMasterMapper.selectOne(stockMasterQueryWrapper);
                master.setStorageEnabled(1);
                //1-空桶 3-退货
                master.setStorageStatus(taskAgvBo.getResult());
                aa = stockMasterMapper.updateById(master);
                if (aa > 0) {
                    //生成出入库履历数据
                    InventoryRecordLog inventoryRecordLog = new InventoryRecordLog();
                    inventoryRecordLog.setId(UUID.randomUUID().toString());
                    inventoryRecordLog.setFlowType(taskAgvBo.getResult());
                    inventoryRecordLog.setBoxId(taskAgvBo.getBoxId());
                    inventoryRecordLog.setItemName(taskAgvBo.getItemName());
                    inventoryRecordLog.setWareHouse(2);
                    inventoryRecordLog.setLocationId(taskAgv.getToSite());
                    inventoryRecordLog.setQty(taskAgvBo.getStockQty());
                    inventoryRecordLog.setCreateTime(new Date());
                    inventoryRecordLogMapper.insert(inventoryRecordLog);
                }
            }
        }
        return aa > 0;
    }

    //根据taskId修改Agv任务-已完成-卸货到临时
    @Override
    public Boolean findByIdOkFour(String taskId) {
        QueryWrapper<TaskAgv> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("task_id", taskId);
        TaskAgv taskAgv = taskAgvMapper.selectOne(queryWrapper);
        taskAgv.setStatus(2);
        int i = taskAgvMapper.updateById(taskAgv);
        //货物状态
        int result = 0;
        int i2 = 0;

        if (i > 0) {
            //删除卸货室 在库 信息
            QueryWrapper<StockFlow> Wrapper = new QueryWrapper<>();
            Wrapper.eq("stock_id",taskAgv.getFlowId()).eq("del_flag",0);
            StockFlow stockFlows = stockFlowLogMapper.selectOne(Wrapper);
            stockFlows.setDelFlag(1);
            stockFlows.setUpdateTime(new Date());
            int i1 = stockFlowLogMapper.updateById(stockFlows);
            if (i1 > 0) {
                //将库位状态改为未占用
                QueryWrapper<StockMaster> queryWrapper1 = new QueryWrapper<>();
                queryWrapper1.eq("location_id", taskAgv.getFromSite());
                StockMaster stockMaster = stockMasterMapper.selectOne(queryWrapper1);

                //货物状态-1-空桶 3-退货
                result = stockMaster.getStorageStatus();

                stockMaster.setStorageEnabled(0);
                stockMaster.setStorageStatus(0);
                i2 = stockMasterMapper.updateById(stockMaster);
                if (i2 > 0) {
                    InventoryRecordLog inventoryRecordLog = new InventoryRecordLog();
                    inventoryRecordLog.setId(UUID.randomUUID().toString());
                    //出库
                    inventoryRecordLog.setFlowType(result);
                    inventoryRecordLog.setBoxId(taskAgv.getBoxId());
                    inventoryRecordLog.setItemName(stockFlows.getItemName());
                    //卸货室
                    inventoryRecordLog.setWareHouse(2);
                    inventoryRecordLog.setLocationId(taskAgv.getFromSite());
                    inventoryRecordLog.setQty(0.0);
                    inventoryRecordLog.setCreateTime(new Date());
                    inventoryRecordLogMapper.insert(inventoryRecordLog);
                } else {
                    return false;
                }
            } else {
                return false;
            }
            //新增临时保管区 在库 信息
            StockFlow stockFlow = new StockFlow();
            stockFlow.setStockId(UUID.randomUUID().toString());
            stockFlow.setBoxId(taskAgv.getBoxId());
            stockFlow.setItemName(stockFlows.getItemName());
            stockFlow.setLocationId(taskAgv.getToSite());
            stockFlow.setWareHouse(1);
            //入库数量再定
            stockFlow.setActualQuantity(0.0);
            //
            stockFlow.setStatus(0);
            stockFlow.setStockFlowType(result);
            stockFlow.setStockTime(new Date());
            stockFlow.setCreateTime(new Date());
            i2 = stockFlowLogMapper.insert(stockFlow);
            if (i2 > 0) {
                //将库位状态改为已占用
                QueryWrapper<StockMaster> queryWrapper1 = new QueryWrapper<>();
                queryWrapper1.eq("location_id", taskAgv.getToSite());
                StockMaster stockMaster = stockMasterMapper.selectOne(queryWrapper1);
                stockMaster.setStorageEnabled(1);
                //货物状态-1-空桶 3-退货
                stockMaster.setStorageStatus(result);

                int i3 = stockMasterMapper.updateById(stockMaster);
                if (i3 > 0) {
                    //生成出入库履历数据
                    InventoryRecordLog inventoryRecordLog = new InventoryRecordLog();
                    inventoryRecordLog.setId(UUID.randomUUID().toString());
                    //货物状态-1-空桶 3-退货
                    inventoryRecordLog.setFlowType(result);
                    //
                    inventoryRecordLog.setBoxId(taskAgv.getBoxId());
                    inventoryRecordLog.setItemName(stockFlows.getItemName());
                    inventoryRecordLog.setWareHouse(1);
                    inventoryRecordLog.setLocationId(taskAgv.getToSite());
                    inventoryRecordLog.setQty(0.00);
                    inventoryRecordLog.setCreateTime(new Date());
                    inventoryRecordLogMapper.insert(inventoryRecordLog);
                } else {
                    return false;
                }
            } else {
                return false;
            }
        }
        return i2 > 0;
    }

    //库位+在库修改
    @Override
    public boolean updateLocation(TaskAgvBo taskAgvBo) {
        int bool;
        //根据fromSite查询出起点位并更改
        QueryWrapper<StockMaster> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("location_id", taskAgvBo.getFromSite());
        StockMaster stockMaster = stockMasterMapper.selectOne(queryWrapper1);
        stockMaster.setStorageEnabled(0);
        stockMaster.setStorageStatus(0);
        bool = stockMasterMapper.updateById(stockMaster);
        if (bool > 0) {
            //根据toSite查询出终点位并更改
            queryWrapper1.eq("location_id", taskAgvBo.getToSite());
            StockMaster master = stockMasterMapper.selectOne(queryWrapper1);
            //库区
            Integer house = master.getWareHouse();
            //类别 1：出库 2：退货  3：空箱
            Integer type = master.getStorageStatus();
            //货物状态
            master.setStorageEnabled(1);
            master.setStorageStatus(stockMaster.getStorageStatus());
            bool = stockMasterMapper.updateById(stockMaster);
            if (bool > 0) {
                //根据flowId查询出在库并更改
                QueryWrapper<StockFlow> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("location_id", taskAgvBo.getFromSite()).eq("del_flag", 0);
                StockFlow stockFlows = stockFlowLogMapper.selectOne(queryWrapper);
                stockFlows.setLocationId(taskAgvBo.getToSite());
                stockFlows.setWareHouse(house);
                stockFlows.setStockFlowType(type);
                stockFlows.setUpdateTime(new Date());
                bool = stockFlowLogMapper.updateById(stockFlows);
            }
        }
        return bool > 0;
    }

    //根据开捆箱id获取ToSite
    @Override
    public InOut selectBoxId(String boxId) {

        QueryWrapper<InOut> inOutQueryWrapper = new QueryWrapper<>();
        inOutQueryWrapper.eq("box_id", boxId)
                .and(wrapper -> (wrapper.eq("status", 0).eq("result",1))
                        .or(wrapper2 -> wrapper2.eq("status", 1).eq("result",3)));
        return inOutMapper.selectOne(inOutQueryWrapper);
    }

    @Override
    public StockMaster selectAgv() {
        //根据参数库区
        QueryWrapper<StockMaster> queryWrapper = new QueryWrapper<>();
        //
        queryWrapper.eq("ware_house", 4)
                .eq("storage_status", 0)
                .eq("storage_enabled", 0)
                .orderByAsc("location_id")
                .orderByAsc("location_goods")
                .last("LIMIT 1");
        return stockMasterMapper.selectOne(queryWrapper);
    }

    /**
     * 查询在库空桶数据
     * @return
     */
    @Override
    public AgvMonious selectAgvNull() {

        QueryWrapper<AgvMonious> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("item_from", 1)
                .eq("status", 0)
                .eq("ware_house", 4)
                .orderByAsc("create_time")
                .eq("del_flag", 0)
                .last("LIMIT 1");
        return agvMoniousMapper.selectOne(queryWrapper);
    }

    /*
     *调和容器AGV生成在库数据-空桶-满桶
     */
    @Override
    public Boolean findAgv(TaskAgvBo taskAgvBo) {
        int i =0;
        QueryWrapper<TaskAgv> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("task_id", taskAgvBo.getTaskId());
        TaskAgv taskAgv = taskAgvMapper.selectOne(queryWrapper);
        taskAgv.setStatus(2);
        i = taskAgvMapper.updateById(taskAgv);
        if (i > 0){
            //生成在库数据
            AgvMonious agvMonious = new AgvMonious();
            agvMonious.setId(UUID.randomUUID().toString());
            agvMonious.setItemNo(taskAgvBo.getItemNo());
            agvMonious.setItemName(taskAgvBo.getItemName());
            //容器状态
            agvMonious.setItemForm(taskAgvBo.getItemForm());
            agvMonious.setLotNo(taskAgvBo.getLotNo());
            agvMonious.setWeight(taskAgvBo.getWeight());
            agvMonious.setRfidTime(taskAgvBo.getRfidTime());
            //调和容器AGV入库库区
            agvMonious.setWareHouse(4);
            //0-未占用 1-已占用
            agvMonious.setStatus(0);
            agvMonious.setCreateBy(123);
            agvMonious.setCreateTime(new Date());

            i = agvMoniousMapper.insert(agvMonious);
            if (i > 0){
                //将库位状态改为已占用
                QueryWrapper<StockMaster> stockMasterQueryWrapper = new QueryWrapper<>();
                stockMasterQueryWrapper.eq("location_id", taskAgv.getToSite());
                StockMaster master = stockMasterMapper.selectOne(stockMasterQueryWrapper);
                master.setStorageEnabled(1);
                //1-空桶 2-满桶
                master.setStorageStatus(taskAgvBo.getResult());
                i = stockMasterMapper.updateById(master);
                if (i > 0) {
                    //生成出入库履历数据
                    InventoryRecordLog inventoryRecordLog = new InventoryRecordLog();
                    inventoryRecordLog.setId(UUID.randomUUID().toString());
                    inventoryRecordLog.setFlowType(taskAgvBo.getResult());
                    inventoryRecordLog.setBoxId(taskAgvBo.getBoxId());
                    inventoryRecordLog.setItemName(taskAgvBo.getItemName());
                    inventoryRecordLog.setWareHouse(4);
                    inventoryRecordLog.setLocationId(taskAgv.getToSite());
                    inventoryRecordLog.setQty(taskAgvBo.getStockQty());
                    inventoryRecordLog.setCreateTime(new Date());
                    inventoryRecordLogMapper.insert(inventoryRecordLog);
                }
            }
        }
        return i>0;
    }

    /**
     * 调和容器agv空桶任务处理
     * @param taskId
     * @return
     */
    @Override
    public Boolean findByIdAGv(String taskId) {
        int i =0;
        QueryWrapper<TaskAgv> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("task_id", taskId);
        TaskAgv taskAgv = taskAgvMapper.selectOne(queryWrapper);
        taskAgv.setStatus(2);
        i = taskAgvMapper.updateById(taskAgv);
        if (i>0){
            QueryWrapper<StockFlow> wrapper = new QueryWrapper<>();
            wrapper.eq("stock_id",taskAgv.getFlowId()).eq("del_flag",0);
            StockFlow stockFlows = stockFlowLogMapper.selectOne(wrapper);
            stockFlows.setDelFlag(1);
            stockFlows.setUpdateTime(new Date());
            i = stockFlowLogMapper.updateById(stockFlows);
            if (i>0){
                QueryWrapper<StockMaster> queryWrapper1 = new QueryWrapper<>();
                queryWrapper1.eq("location_id", stockFlows.getLocationId());
                StockMaster stockMaster = stockMasterMapper.selectOne(queryWrapper1);
                stockMaster.setStorageEnabled(0);
                stockMaster.setStorageStatus(0);
                i = stockMasterMapper.updateById(stockMaster);
                if (i>0){
                    //生成出入库履历数据
                    InventoryRecordLog inventoryRecordLog = new InventoryRecordLog();
                    inventoryRecordLog.setId(UUID.randomUUID().toString());
                    inventoryRecordLog.setFlowType(stockFlows.getStockFlowType());
                    inventoryRecordLog.setBoxId(stockFlows.getBoxId());
                    inventoryRecordLog.setItemName(stockFlows.getItemName());
                    inventoryRecordLog.setWareHouse(4);
                    inventoryRecordLog.setLocationId(stockFlows.getLocationId());
                    inventoryRecordLog.setQty(stockFlows.getActualQuantity());
                    inventoryRecordLog.setCreateTime(new Date());
                    inventoryRecordLogMapper.insert(inventoryRecordLog);
                }
            }
        }
        return i>0;
    }

    @Override
    public StockFlow selectById(String boxId) {
        QueryWrapper<StockFlow> queryWrapper = new QueryWrapper<>();
        //生药卸货室 -- 终点裁切室 用法
        queryWrapper.eq("box_id", boxId).eq("del_flag", 0);

        return stockFlowLogMapper.selectOne(queryWrapper);
    }
}
